package com.zjy.heath_ms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.jdbc.StringUtils;
import com.zjy.heath_ms.configure.config.AliyunOSSConfig;
import com.zjy.heath_ms.entity.QueryPageBean;
import com.zjy.heath_ms.mapper.SetMealCheckGroupMapper;
import com.zjy.heath_ms.mapper.SetMealMapper;
import com.zjy.heath_ms.pojo.Setmeal;
import com.zjy.heath_ms.pojo.SetmealCheckgroup;
import com.zjy.heath_ms.pojo.vo.SetMealDto;
import com.zjy.heath_ms.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;


/**
 * 项目名 heath_ms
 * 类名  SetMealServiceImpl
 * 作者 lzq
 * 创建时间  2025/5/25 12:23
 */
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {

    @Autowired
    private SetMealCheckGroupMapper setMealCheckGroupMapper;

    @Autowired
    private AliyunOSSUtil aliyunOssUtil;

    @Autowired
    private AliyunOSSConfig aliyunOSSConfig;
    @Override
    public Page<Setmeal> findPage(QueryPageBean queryPageBean) {
        // 查询条件
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            setmealLambdaQueryWrapper.like(Setmeal::getCode, queryString)
                    .or()
                    .like(Setmeal::getName, queryString)
                    .or()
                    .like(Setmeal::getHelpCode, queryString);
        }
        // 分页条件
        Page<Setmeal> setmealPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 执行查询
        baseMapper.selectPage(setmealPage, setmealLambdaQueryWrapper);
        return setmealPage;
    }

    @Override
    public boolean saveOne(SetMealDto setMealDto) {
        //添加图片到阿里云oss存储
        String imageUrl = aliyunOssUtil.upload(setMealDto.getImageFile(), aliyunOSSConfig.getPathSetMeal());
        setMealDto.setImg(imageUrl);

        // 添加套餐基本信息
        baseMapper.insert(setMealDto);
        // 添加套餐与检查组的关系
        for (Integer checkGroupId : setMealDto.getCheckGroupIds()) {
            setMealCheckGroupMapper.insert(new SetmealCheckgroup(setMealDto.getId(), checkGroupId));
        }
        return true;
    }
}
